package com.avic.rong.search.adapter.searchResultFragmentsAdapters;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.avic.rong.BaseActivity;
import com.avic.rong.R;
import com.avic.rong.activity.ArticleDetailActivity;
import com.avic.rong.activity.VideoDetailActivity;
import com.avic.rong.bean.show_bean.DetailBean;
import com.avic.rong.bean.show_bean.EsInfoBean;
import com.avic.rong.fragment.showapi.DetailApi;
import com.avic.rong.goodsSort.bean.showRvBean.GoodsSortSearchAllBzBean;
import com.avic.rong.search.activity.SearchHomePageActivity;
import com.avic.rong.utils.StringUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Chenjiaxin on 2019/7/20 12:55
 * E-Mail Address：16601203965@163.com
 */
public class SearchArticleRvAdapter extends RecyclerView.Adapter<SearchArticleRvAdapter.SearchArticleRvViewHolder> {

    private Context context;
    private LayoutInflater inflater;
    private List<GoodsSortSearchAllBzBean.DataBean> list;
    private static final int STYPE_NO_PICTURE = 0;
    private static final int STYPE_ONE_PICTURE = 1;
    private static final int STYPE_THREE_PICTURE = 3;
    private ArrayList<EsInfoBean> esInfoBeans;
    private int view_num = 0;

    public SearchArticleRvAdapter(Context context, List<GoodsSortSearchAllBzBean.DataBean> list, ArrayList<EsInfoBean> esInfoBeans) {
        this.context = context;
        this.list = list;
        this.inflater = LayoutInflater.from(context);
        this.esInfoBeans = esInfoBeans;
    }

    @NonNull
    @Override
    public SearchArticleRvViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.view_command, viewGroup, false);
        if (i == STYPE_ONE_PICTURE) {
            view = LayoutInflater.from(context).inflate(R.layout.view_command_one_pic_search, viewGroup, false);
        } else if (i == STYPE_THREE_PICTURE) {
            view = LayoutInflater.from(context).inflate(R.layout.view_command_three_pic, viewGroup, false);
        }

        return new SearchArticleRvViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull SearchArticleRvViewHolder holder, int i) {


        if (list != null) {


            GoodsSortSearchAllBzBean.DataBean datas = list.get(i);
            String coverImgOne = datas.getCoverImgOne();
            String coverImgtwo = datas.getCoverImgTwo();
            String img = datas.getImg();
            int id = datas.getDataId();
            if (esInfoBeans != null) {
                for (int j = 0; j < esInfoBeans.size(); j++) {
                    if (id == esInfoBeans.get(j).getCont_id()) {
                        view_num = esInfoBeans.get(j).getView_num();
                    }
                }
            }
            if (coverImgOne != null && coverImgtwo != null) {
                if (!coverImgOne.equals("") && !coverImgtwo.equals("")) {
                    holder.three_before.setText(datas.getPostTime());
                    holder.three_readsize.setText(view_num + "阅读");
                    SpannableString spannableString = new SpannableString(list.get(i).getTitle());
                    if (list.get(i).getTitle() != null) {
                        int startIndex = list.get(i).getTitle().toLowerCase().indexOf(SearchHomePageActivity.nowSearchStr.toLowerCase());
                        if (startIndex >= 0) {
                            ForegroundColorSpan colorSpan = new ForegroundColorSpan(ContextCompat.getColor(context, R.color.color_f23030));
                            spannableString.setSpan(colorSpan, startIndex, startIndex + SearchHomePageActivity.nowSearchStr.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
                            holder.three_tv_category_name.setText(spannableString);
                        } else {
                            holder.three_tv_category_name.setText(list.get(i).getTitle());
                        }
                    }
                    holder.three_ribao.setText(datas.getAuthor());
                    Glide.with(context).load(datas.getImg()).apply(RequestOptions.bitmapTransform(new RoundedCorners(1))).into(holder.three_oneimage);
                    Glide.with(context).load(datas.getCoverImgOne()).apply(RequestOptions.bitmapTransform(new RoundedCorners(1))).into(holder.three_twoimage);
                    Glide.with(context).load(datas.getCoverImgTwo()).apply(RequestOptions.bitmapTransform(new RoundedCorners(1))).into(holder.three_threeimage);
                } else if (!img.equals("")) {
                    holder.one_readsize.setText(view_num + "阅读");
                    holder.one_before.setText(datas.getPostTime());
                    holder.one_ridu.setText(datas.getAuthor());
                    SpannableString spannableString = new SpannableString(list.get(i).getTitle());
                    if (list.get(i).getTitle() != null) {
                        int startIndex = list.get(i).getTitle().toLowerCase().indexOf(SearchHomePageActivity.nowSearchStr.toLowerCase());
                        if (startIndex >= 0) {
                            ForegroundColorSpan colorSpan = new ForegroundColorSpan(ContextCompat.getColor(context, R.color.color_f23030));
                            spannableString.setSpan(colorSpan, startIndex, startIndex + SearchHomePageActivity.nowSearchStr.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
                            holder.one_tv_category_name.setText(spannableString);
                        } else {
                            holder.one_tv_category_name.setText(list.get(i).getTitle());
                        }
                    }
                    Glide.with(context).load(datas.getImg()).apply(RequestOptions.bitmapTransform(new RoundedCorners(1))).into(holder.one_oneimage);
                } else {
                    holder.no_readsize.setText(view_num + "阅读");
                    holder.no_ribao.setText(datas.getAuthor());
                    holder.no_before.setText(datas.getPostTime());
                    SpannableString spannableString = new SpannableString(list.get(i).getTitle());
                    if (list.get(i).getTitle() != null) {
                        int startIndex = list.get(i).getTitle().toLowerCase().indexOf(SearchHomePageActivity.nowSearchStr.toLowerCase());
                        if (startIndex >= 0) {
                            ForegroundColorSpan colorSpan = new ForegroundColorSpan(ContextCompat.getColor(context, R.color.color_f23030));
                            spannableString.setSpan(colorSpan, startIndex, startIndex + SearchHomePageActivity.nowSearchStr.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
                            holder.no_tv_category_name.setText(spannableString);
                        } else {
                            holder.no_tv_category_name.setText(list.get(i).getTitle());
                        }
                    }
                }
            } else if (img != null) {
                if (!img.equals("")) {
                    holder.one_readsize.setText(view_num + "阅读");
                    holder.one_before.setText(datas.getPostTime());
                    holder.one_ridu.setText(datas.getAuthor());
                    SpannableString spannableString = new SpannableString(list.get(i).getTitle());
                    if (list.get(i).getTitle() != null) {
                        int startIndex = list.get(i).getTitle().toLowerCase().indexOf(SearchHomePageActivity.nowSearchStr.toLowerCase());
                        if (startIndex >= 0) {
                            ForegroundColorSpan colorSpan = new ForegroundColorSpan(ContextCompat.getColor(context, R.color.color_f23030));
                            spannableString.setSpan(colorSpan, startIndex, startIndex + SearchHomePageActivity.nowSearchStr.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
                            holder.one_tv_category_name.setText(spannableString);
                        } else {
                            holder.one_tv_category_name.setText(list.get(i).getTitle());
                        }
                    }
                    Glide.with(context).load(datas.getImg()).apply(RequestOptions.bitmapTransform(new RoundedCorners(1))).into(holder.one_oneimage);
                } else {
                    holder.no_readsize.setText(view_num + "阅读");
                    holder.no_before.setText(datas.getPostTime());
                    holder.no_ribao.setText(datas.getAuthor());
                    SpannableString spannableString = new SpannableString(list.get(i).getTitle());
                    if (list.get(i).getTitle() != null) {
                        int startIndex = list.get(i).getTitle().toLowerCase().indexOf(SearchHomePageActivity.nowSearchStr.toLowerCase());
                        if (startIndex >= 0) {
                            ForegroundColorSpan colorSpan = new ForegroundColorSpan(ContextCompat.getColor(context, R.color.color_f23030));
                            spannableString.setSpan(colorSpan, startIndex, startIndex + SearchHomePageActivity.nowSearchStr.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
                            holder.no_tv_category_name.setText(spannableString);
                        } else {
                            holder.no_tv_category_name.setText(list.get(i).getTitle());
                        }
                    }
                }
            } else {
                holder.no_readsize.setText(view_num + "阅读");
                holder.no_before.setText(datas.getPostTime());
                holder.no_ribao.setText(datas.getAuthor());
                SpannableString spannableString = new SpannableString(list.get(i).getTitle());
                if (list.get(i).getTitle() != null) {
                    int startIndex = list.get(i).getTitle().toLowerCase().indexOf(SearchHomePageActivity.nowSearchStr.toLowerCase());
                    if (startIndex >= 0) {
                        ForegroundColorSpan colorSpan = new ForegroundColorSpan(ContextCompat.getColor(context, R.color.color_f23030));
                        spannableString.setSpan(colorSpan, startIndex, startIndex + SearchHomePageActivity.nowSearchStr.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
                        holder.no_tv_category_name.setText(spannableString);
                    } else {
                        holder.no_tv_category_name.setText(list.get(i).getTitle());
                    }
                }
            }

            holder.dataBean = list.get(i);

        }
    }

    @Override
    public int getItemViewType(int position) {
        String coverImgOne = list.get(position).getCoverImgOne();
        String coverImgTwo = list.get(position).getCoverImgTwo();
        String img = list.get(position).getImg();

        if (!StringUtils.isEmpty(coverImgOne) && !StringUtils.isEmpty(coverImgTwo)) {
            return STYPE_THREE_PICTURE;
        } else if (!StringUtils.isEmpty(img)) {
            return STYPE_ONE_PICTURE;
        }
        return STYPE_NO_PICTURE;

    }

    @Override
    public int getItemCount() {
        if (list != null) {
            return list.size();
        }
        return 0;
    }

    class SearchArticleRvViewHolder extends RecyclerView.ViewHolder {
        TextView tv_category_name;
        ImageView one_oneimage, three_twoimage, three_oneimage, three_threeimage;
        TextView tv_content;//无图提示
        TextView no_before, no_ribao, no_readsize, no_tv_category_name, one_before, one_tv_category_name, one_toutiao, one_ridu, one_readsize,
                three_before, three_readsize, three_ribao, three_toutiao, three_tv_category_name;
        GoodsSortSearchAllBzBean.DataBean dataBean;

        public SearchArticleRvViewHolder(@NonNull View itemView) {
            super(itemView);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //内容详情z
                    DetailApi detailApi = new DetailApi((RxAppCompatActivity) context, DetailhttpOnNextListener, "", dataBean.getDataId() + "");
                    detailApi.setCache(false);
                    HttpManager.getInstance().doHttpDeal(detailApi);
                }
            });

            tv_category_name = itemView.findViewById(R.id.tv_category_name);
            no_before = itemView.findViewById(R.id.no_before);
            no_ribao = itemView.findViewById(R.id.no_ribao);
            no_readsize = itemView.findViewById(R.id.no_readsize);
            no_tv_category_name = itemView.findViewById(R.id.no_tv_category_name);

            one_before = itemView.findViewById(R.id.one_before);
            one_oneimage = itemView.findViewById(R.id.one_oneimage);
            one_readsize = itemView.findViewById(R.id.one_readsize);
            one_ridu = itemView.findViewById(R.id.one_ribao);
            //   one_toutiao = itemView.findViewById(R.id.one_toutiao);
            one_tv_category_name = itemView.findViewById(R.id.one_tv_category_name);

            three_before = itemView.findViewById(R.id.three_before);
            three_oneimage = itemView.findViewById(R.id.three_oneimage);
            three_twoimage = itemView.findViewById(R.id.three_twoimage);
            three_threeimage = itemView.findViewById(R.id.three_threeimage);
            three_readsize = itemView.findViewById(R.id.three_readsize);
            three_ribao = itemView.findViewById(R.id.three_ribao);
            //   three_toutiao = itemView.findViewById(R.id.three_toutiao);
            three_tv_category_name = itemView.findViewById(R.id.three_tv_category_name);
        }

        //内容详情
        HttpOnNextListener<BaseResultEntity> DetailhttpOnNextListener = new HttpOnNextListener<BaseResultEntity>() {
            @Override
            public void onNext(BaseResultEntity baseResultEntity) {
                String code = baseResultEntity.getCode();
                if (code.equals("200")) {
                    DetailBean detailBean = (DetailBean) baseResultEntity.getData();
                    List<DetailBean.OtherInfoBean> otherInfo = detailBean.getOtherInfo();
                    if (5 == otherInfo.get(0).getTypeFlg()) {
                        Intent intent = new Intent(context, VideoDetailActivity.class);
                        intent.putExtra("contId", dataBean.getDataId());
                        context.startActivity(intent);
                    } else {
                        Intent intent = new Intent(context, ArticleDetailActivity.class);
                        intent.putExtra("contId", dataBean.getDataId());
                        context.startActivity(intent);
                    }


                }

            }

        };
    }
}
